<?php
namespace app\cms\model;
/* *
 * 数据库表管理功能
 * */
class module extends \app\base\model\AdminModel{
    protected $table ='cms_module';  //模型表

    //数据库表
    public function tables($mid){
        $mid = intval($mid);
        $tables = $this->where(array('id'=>$mid))->field('tables')->find();
        return $tables['tables'];
    }
    
    //列表
    public function _select($where,$order = NULL,$field = NULL){
       return $this->where($where)->field($field)->order($order)->select();
    }

    //查询单个
    public function _find($where,$field = NULL){
       return $this->where($where)->field($field)->find();
    }

    //删除模型->数据库表
    public function _delete($fid){
        $channel = model('channel')->_find(array('mid' =>$fid));
        if($channel){
            return '删除模型失败,请先删除已绑定的栏目';
        }
        $field = model('field')->_find(array('mid' => $fid));
        if($field){
            return '删除模型失败,请先删除模型字段';
        }
        $tables = $this->where(array('id' => $fid))->field('tables')->find();  //获取当前表名
        $sql = "DROP TABLE {pre}".$tables['tables'];
        try{
            $this->execute($sql);
        } catch( \Exception $e ){
            return '执行SQL语句有误';
        }
        $result = $this->where(array('id' => $fid))->delete();
        if($result){
            return FALSE;
        }else{
            return '删除模型失败';
        }
    }
    //增加模型->添加数据库表
    public function addModule($data = array()){
        $condition['tables'] = $data['tables'];
        $result = $this->where($condition)->find($condition); 
        if($result){
            return FALSE;
        }
        $type = intval($data['type']);
        if($type == 1){//资源模型扩展
            $sql = "CREATE TABLE IF NOT EXISTS {pre}".$data['tables']."(
            `id` int(11) NOT NULL AUTO_INCREMENT,    
            `content_id` int(11) NOT NULL,
            `channel_id` int(11) NOT NULL,
            `title` varchar(150) DEFAULT NULL,
            `url` varchar(150) DEFAULT NULL,
            `sort` int(10) DEFAULT  NULL,
            `views` int(10) DEFAULT  NULL,
            `editor` varchar(30) DEFAULT NULL,
            `fontcolor` varchar(10) DEFAULT NULL,
            `uptime` int(10) DEFAULT NULL,
            PRIMARY KEY  (`id`)
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8";
        }elseif($type == 2){//栏目扩展
            $sql = "CREATE TABLE IF NOT EXISTS {pre}".$data['tables']."(
            `channel_id` int(10) NOT NULL,
            PRIMARY KEY  (`channel_id`)
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8";
        }else{//资源模型主表
            $sql = "CREATE TABLE IF NOT EXISTS {pre}".$data['tables']."(
            `content_id` int(11) NOT NULL,   
            `channel_id` int(11) NOT NULL,
            PRIMARY KEY  (`content_id`)
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8";
        }
        try{
            $this->execute($sql);
        } catch( \Exception $e ){
            return FALSE;
        }
        $result = $this->data($data)->insert();
        if($result){
            return TRUE;
        }else{
            return FALSE;
        }
    }
    //编辑模型->数据库表->数据库表字段
    public function editModule($fid,$data = array()){
        $condition['id'] = $fid;
        $result = $this->where($condition)->data($data)->update();
        if($result){
            return TRUE;
        }else{
            return FALSE;
        }
    }
}